package restaurant.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;

import com.toedter.calendar.JDateChooser;

/**
 * This JPanel is used to add or edit an Offer.
 * 
 * @author Odessa
 */
@SuppressWarnings("serial")
public class OfferPanel extends JPanel
{

	/** The offer number text field. */
	private JTextField offerNoField;

	/** The description text field. */
	private JTextField descField;

	/** The discount text field. */
	private JTextField discountField;

	/** The start date chooser. */
	private JDateChooser startDateChooser;

	/** The end date chooser. */
	private JDateChooser endDateChooser;

	/** This panel. */
	private JPanel thisPanel = this;

	/** The option of whether the offer is being added or edited. */
	private int option;

	private Date now;



	/**
	 * Creates the panel.
	 */
	public OfferPanel() {
		initComponents();
		option = ActionsPanel.ADD;
	}



	/**
	 * Creates the panel and instantiates the text fields.
	 * 
	 * @param id the offer number.
	 * @param start the offer's current start date.
	 * @param end the offer's current end date.
	 * @param desc the current description of the offer.
	 * @param discount the current discount of the offer.
	 */
	public OfferPanel(int id, Date start, Date end, String desc, int discount) {
		initComponents();
		option = ActionsPanel.EDIT;

		offerNoField.setText(Integer.toString(id));
		startDateChooser.setDate(start);
		endDateChooser.setDate(end);
		descField.setText(Integer.toString(discount));
		discountField.setText(desc);
	}



	/**
	 * Initializes the components.
	 */
	private void initComponents()
	{
		Calendar today = Calendar.getInstance();
		today.add(Calendar.DATE, -1);
		now = today.getTime();

		JLabel offerNoLabel = new JLabel("Offer number");

		offerNoField = new JTextField();
		offerNoField.setEnabled(false);
		offerNoField.setColumns(5);

		JLabel descLabel = new JLabel("Description");

		descField = new JTextField();
		descField.setColumns(30);

		JLabel discountLabel = new JLabel("Discount");

		discountField = new JTextField();
		discountField.setColumns(20);

		JLabel startLabel = new JLabel("Starting");

		startDateChooser = new JDateChooser();
		startDateChooser.setDateFormatString("MM/dd/yyyy");
		startDateChooser.setLocale(Locale.US);

		JLabel endLabel = new JLabel("Ending");

		endDateChooser = new JDateChooser();
		endDateChooser.setDateFormatString("MM/dd/yyyy");
		endDateChooser.setLocale(Locale.US);

		// Create the buttons and add them to the buttonPanel
		JButton saveButton = new JButton("Save");
		saveButton.setPreferredSize(new Dimension(73, 26));
		saveButton.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e)
			{
				saveButtonActionPerformed(e);
			}
		});

		JButton cancelButton = new JButton("Cancel");
		cancelButton.setPreferredSize(new Dimension(73, 26));
		cancelButton.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent evt)
			{
				JFrame frame = (JFrame) SwingUtilities.getRoot(thisPanel);
				frame.dispose();
			}
		});

		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		buttonPanel.add(saveButton);
		buttonPanel.add(cancelButton);

		// Create the group layout for this panel
		GroupLayout layout = new GroupLayout(this);
		this.setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										layout.createParallelGroup(
												GroupLayout.Alignment.TRAILING)
												.addComponent(
														buttonPanel,
														GroupLayout.PREFERRED_SIZE,
														GroupLayout.DEFAULT_SIZE,
														GroupLayout.PREFERRED_SIZE)
												.addGroup(
														layout.createSequentialGroup()
																.addGroup(
																		layout.createParallelGroup(
																				GroupLayout.Alignment.LEADING)
																				.addComponent(
																						offerNoLabel)
																				.addComponent(
																						descLabel)
																				.addComponent(
																						discountLabel)
																				.addComponent(
																						startLabel)
																				.addComponent(
																						endLabel))
																.addPreferredGap(
																		LayoutStyle.ComponentPlacement.RELATED)
																.addGroup(
																		layout.createParallelGroup(
																				GroupLayout.Alignment.LEADING)
																				.addComponent(
																						offerNoField,
																						GroupLayout.PREFERRED_SIZE,
																						GroupLayout.DEFAULT_SIZE,
																						GroupLayout.PREFERRED_SIZE)
																				.addComponent(
																						descField,
																						GroupLayout.PREFERRED_SIZE,
																						GroupLayout.DEFAULT_SIZE,
																						GroupLayout.PREFERRED_SIZE)
																				.addComponent(
																						startDateChooser,
																						GroupLayout.PREFERRED_SIZE,
																						GroupLayout.DEFAULT_SIZE,
																						GroupLayout.PREFERRED_SIZE)
																				.addComponent(
																						endDateChooser,
																						GroupLayout.PREFERRED_SIZE,
																						GroupLayout.DEFAULT_SIZE,
																						GroupLayout.PREFERRED_SIZE)
																				.addComponent(
																						discountField,
																						GroupLayout.PREFERRED_SIZE,
																						GroupLayout.DEFAULT_SIZE,
																						GroupLayout.PREFERRED_SIZE))))
								.addContainerGap(53, Short.MAX_VALUE)));

		layout.linkSize(SwingConstants.HORIZONTAL, new java.awt.Component[] {
				descField, discountField, startDateChooser, endDateChooser });

		layout.setVerticalGroup(layout.createParallelGroup(
				GroupLayout.Alignment.LEADING).addGroup(
				layout.createSequentialGroup()
						.addContainerGap()
						.addGroup(
								layout.createParallelGroup(
										GroupLayout.Alignment.BASELINE)
										.addComponent(offerNoLabel)
										.addComponent(offerNoField,
												GroupLayout.PREFERRED_SIZE,
												GroupLayout.DEFAULT_SIZE,
												GroupLayout.PREFERRED_SIZE))
						.addPreferredGap(
								LayoutStyle.ComponentPlacement.UNRELATED)
						.addGroup(
								layout.createParallelGroup(
										GroupLayout.Alignment.BASELINE)
										.addComponent(descLabel)
										.addComponent(descField,
												GroupLayout.PREFERRED_SIZE,
												GroupLayout.DEFAULT_SIZE,
												GroupLayout.PREFERRED_SIZE))
						.addPreferredGap(
								LayoutStyle.ComponentPlacement.UNRELATED)
						.addGroup(
								layout.createParallelGroup(
										GroupLayout.Alignment.BASELINE)
										.addComponent(discountLabel)
										.addComponent(discountField,
												GroupLayout.PREFERRED_SIZE,
												GroupLayout.DEFAULT_SIZE,
												GroupLayout.PREFERRED_SIZE))
						.addPreferredGap(
								LayoutStyle.ComponentPlacement.UNRELATED)
						.addGroup(
								layout.createParallelGroup(
										GroupLayout.Alignment.BASELINE)
										.addComponent(startLabel)
										.addComponent(startDateChooser,
												GroupLayout.PREFERRED_SIZE,
												GroupLayout.DEFAULT_SIZE,
												GroupLayout.PREFERRED_SIZE))
						.addPreferredGap(
								LayoutStyle.ComponentPlacement.UNRELATED)
						.addGroup(
								layout.createParallelGroup(
										GroupLayout.Alignment.BASELINE)
										.addComponent(endLabel)
										.addComponent(endDateChooser,
												GroupLayout.PREFERRED_SIZE,
												GroupLayout.DEFAULT_SIZE,
												GroupLayout.PREFERRED_SIZE))
						.addGap(18, 18, 18)
						.addComponent(buttonPanel, GroupLayout.PREFERRED_SIZE,
								GroupLayout.DEFAULT_SIZE,
								GroupLayout.PREFERRED_SIZE)
						.addContainerGap(94, Short.MAX_VALUE)));
	}



	/**
	 * The event handler for the OK button.
	 * 
	 * @param evt the action event.
	 */
	protected void saveButtonActionPerformed(ActionEvent evt)
	{
		// TODO Auto-generated method stub
		JFrame frame = (JFrame) SwingUtilities.getRoot(this);

		try {
			String offer = offerNoField.getText();
			String description = descField.getText();
			String discount = discountField.getText();
			Date start = startDateChooser.getDate();
			Date end = endDateChooser.getDate();
			System.out.println("current: " + now);
			System.out.println("start: " +
					startDateChooser.getDate().toString());
			System.out.println("end: " + endDateChooser.getDate().toString());

			if (end.before(start)) {
				JOptionPane
						.showMessageDialog(
								frame,
								"The ending date cannot be earlier than the staring date.",
								"Date error", JOptionPane.ERROR_MESSAGE,
								Restaurant.ERROR_ICON);
			}
			else if (now.after(start)) {
				JOptionPane.showMessageDialog(frame,
						"The starting date cannot be earlier than today.",
						"Date error", JOptionPane.ERROR_MESSAGE,
						Restaurant.ERROR_ICON);
			}
			else if (now.after(end)) {
				JOptionPane.showMessageDialog(frame,
						"The ending date cannot be earlier than today.",
						"Date error", JOptionPane.ERROR_MESSAGE,
						Restaurant.ERROR_ICON);
			}

			if (option == ActionsPanel.ADD) {
				Restaurant.actions.addOffer(start, end, description,
						Double.parseDouble(discount));
			}
			else if (option == ActionsPanel.EDIT) {
				Restaurant.actions.updateOffer(Integer.parseInt(offer), start,
						end, description, Double.parseDouble(discount));
			}

			Restaurant.actions.addOffer(start, end, description,
					Double.parseDouble(discount));
			frame.dispose();
		}
		catch (NullPointerException npe) {
			JOptionPane.showMessageDialog(frame,
					"Please enter both a starting and ending date.",
					"Date error", JOptionPane.ERROR_MESSAGE,
					Restaurant.ERROR_ICON);
		}

	}



	/**
	 * The main method.
	 * 
	 * @param args the arguments
	 */
	public static void main(String[] args)
	{
		JFrame f = new JFrame("Date trial");
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.getContentPane().setLayout(new BorderLayout());
		f.getContentPane().add(new OfferPanel());
		f.pack();
		f.setVisible(true);
	}
}
